package cn.acewill.yxd.yunxundian.ui.sign.Presenter;

import android.app.ProgressDialog;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.ChongFuKouFen;
import cn.acewill.yxd.yunxundian.model.CommonMapBean;
import cn.acewill.yxd.yunxundian.model.Employee;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.Shop;
import cn.acewill.yxd.yunxundian.model.reportViewBean;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.model.viewReportBean;
import cn.acewill.yxd.yunxundian.pubClass.OkHttpManger;
import cn.acewill.yxd.yunxundian.pubClass.RMainUtils;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.MainActivity;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.MEvaluationView;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.PreViewLoadView;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.PreviewPresenter;

/**
 * Created by xxx on 2018/2/1.
 */

public class PreviewPresenterIml implements PreviewPresenter {
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private PreViewLoadView loadView;
    private List<RMain> rMains;

    public PreviewPresenterIml(PreViewLoadView loadView){
        this.loadView = loadView;
    }

    /**
     * 缺少地区，行动计划人的名称
     * @param schId
     */
    @Override
    public void loadReportViewData(int schId) {
        if (loadView!=null){
            ArrayList<reportViewBean> sourceList = new ArrayList<>();
            reportViewBean dataBean = new reportViewBean();

            ArrayList<reportViewBean.JibenxinxiBean> jiList = new ArrayList<>();
            ArrayList<reportViewBean.ZhutiBean> zhuList = new ArrayList<>();
            rMains = RMainUtils.getInstance().loadRMins(schId);
            //拼装数据
            try {
                List<tempLostRR> rrList = db.selector(tempLostRR.class).where("schId","=",schId)
                        .and("empId","=", MainActivity.id).findAll();
                    //组装基本数据
                    Schedule schedule = db.selector(Schedule.class).where("ID","=",schId).findFirst();
                    String score = pubClass.calScoreBySch(schId);
                    String level = "";
                    String titleScore = "";
                    if (score.contains("(")&&score.contains(")")){
                        level = score.substring(score.length()-2,score.length()-1);
                        titleScore = score.substring(0,score.length()-3);
                    }
                    Employee emp = db.selector(Employee.class).where("ID","=",MainActivity.id).findFirst();
                    if (schedule!=null){
                        Shop shop = db.selector(Shop.class).where("ID","=",schedule.getShopID()).findFirst();
                        String shopName = "";
                        String shopNum = "";
                        String areaName = "----";
                        if (shop!=null){
                            shopName = shop.getShopName();
                            shopNum = shop.getShopNum();
                        }
                        reportViewBean.JibenxinxiBean jiBen = new reportViewBean.JibenxinxiBean(schedule.getTitle()
                                ,schedule.getRealStartTime(),schedule.getRealEndTime(),level,Double.parseDouble(titleScore),emp.getFirstName(),shopName,
                                areaName,rMains.get(0).getQ().getReportName(),shopNum,12.5);
                        jiList.add(jiBen);
                       dataBean.setJibenxinxi(jiList);

                        //组装评估主体数据
                        if (rrList!=null&&rrList.size()>0) {
                            for (tempLostRR rr : rrList) {
                                List<String> videoList = new ArrayList<>();
                                List<String> jpgList = new ArrayList<>();
                                if (rMains != null) {
                                    RMain.TablesBean.RowsBean rowsBean = RMainUtils.getInstance().getRowByMInId(rMains, rr.getRRowId());
                                    RMain.TablesBean.Q1Bean q1Bean = RMainUtils.getInstance().getQ1ByMInId(rMains, rr.getRRowId());
                                    String q1Name = "";
                                    String qDesc = "";
                                    int orderValue = 1;
                                    if (q1Bean != null) {
                                        q1Name = q1Bean.getReportTableName();
                                        qDesc = q1Bean.getDescription();
                                        orderValue = q1Bean.getOrderValue();
                                    }
                                    if (rr.getJpgStr() != null && rr.getJpgStr().length() > 0) {
                                        String[] path = rr.getJpgStr().split(",");
                                        Collections.addAll(jpgList, path);
                                    }
                                    if (rr.getVideoStr() != null && rr.getVideoStr().length() > 0) {
                                        String[] path = rr.getVideoStr().split(",");
                                        Collections.addAll(videoList, path);
                                    }

                                    if (rowsBean != null) {
                                        int shifoushifen = 0;
                                        String shifenName = "";
                                        for (RMain.TablesBean.RowsBean.RItemsBean item : rowsBean.getRItems()) {
                                            if (item.getID() == rr.getSelectItemId()) {
                                                shifenName = item.getName();
                                                break;
                                            }
                                        }
                                        //如果是失分项标记为1
                                        if (rr.getScore() < rowsBean.getQ2().getMaxScore()) {
                                            shifoushifen = 1;
                                        }
                                        reportViewBean.ZhutiBean zhuti = new reportViewBean.ZhutiBean(rowsBean.getQ2().getReportRowName()
                                                , rowsBean.getQ2().getSName(), shifenName, rowsBean.getQ2().getItemType(),
                                                rr.getScore(), rowsBean.getQ2().getMaxScore(), rr.getScore(), shifoushifen,
                                                rowsBean.getQ2().getIsPoint(), q1Name, qDesc, rowsBean.getQ2().getDescription(),
                                                rr.getDescStr(), videoList,jpgList, rr.isFbIsCheck(), rr.getXindongId(), rr.getXiongdongTime(),rr.getRRowId(),schId,orderValue);
                                        zhuList.add(zhuti);
                                    }

                                }

                            }
                            dataBean.setZhuti(zhuList);
                        }
                        CommonMapBean cmb = db.selector(CommonMapBean.class).where("CmType", "=", "门店签名")
                                .and("IndexNum", "=", schId).findFirst();
                        String qianming = "";
                        if (cmb!=null){
                            qianming = cmb.getCmValue();
                        }
                        dataBean.setQianming(qianming);
                        sourceList.add(dataBean);
                }
            } catch (DbException e) {
                e.printStackTrace();
            }

            loadView.getTempData(sourceList);
        }
    }

    //加载人员信息
    @Override
    public void loadtomanData(int schId) {

        Map<String,String> map=new HashMap<>();
        map.put("task","getShopTodoEmpList");
        map.put("schid",schId+"");
        Log.e("schid",schId+"");
        try {
            OkHttpManger.getInstance().postAsync(pubClass.Server_namehzm, new OkHttpManger.MyCallback() {
                @Override
                public void onSuccess(String result) {
                    String[] array=result.split("\\|");
                    if(array[0].equals("200")){
                        //
                        Gson gson=new Gson();
                        Type typeList=new TypeToken<ArrayList<Employee>>(){}.getType();
                        List<Employee> emp=gson.fromJson(pubClass.convertToArrayJson(result),typeList);
                        loadView.getToManData(emp);

                    }

                }
                @Override
                public void onFailture() {

                }
            },map);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void loadAreaData(final int schId) {
        int areaId = 0;
        try {
            Schedule schedule = db.selector(Schedule.class).where("ID","=",schId).findFirst();
            if (schedule!=null){
                Shop shop = db.selector(Shop.class).where("ID","=",schedule.getShopID()).findFirst();
                if (shop!=null){
                    areaId = shop.getAreaId();
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        Map<String,String> map = new HashMap<>();
        map.put("task","getAreaName");
        map.put("areaid",areaId+"");

        try {
            OkHttpManger.getInstance().postAsync(pubClass.Server_namehzm, new OkHttpManger.MyCallback() {
                @Override
                public void onSuccess(String result) {
                    String[] array=result.split("\\|");
                    if (array[0].equals("200")){
                        if (!array[3].equals("noJson"))
                            loadView.getAreaName(array[3]);
                    }
                }
                @Override
                public void onFailture() {

                }
            },map);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void loadkouFenData(int schId) {
        HashMap<String,String> map = new HashMap<>();
        map.put("task","chongfukoufen");
        map.put("schid",schId+"");
        try {
            OkHttpManger.getInstance().postAsync(pubClass.Server_namehzm, new OkHttpManger.MyCallback() {
                @Override
                public void onSuccess(String result) {
                    String[] array = result.split("\\|");
                    if (array[0].equals("200")) {
                        //noJson
                        if (array[3].equals("noJson")){
                            return;
                        }
                        Gson gson = new Gson();
                        Type typeList = new TypeToken<ArrayList<ChongFuKouFen>>() {
                        }.getType();

                        ArrayList<ChongFuKouFen> koufens = gson.fromJson(pubClass.convertToArrayJson(result), typeList);
                        loadView.getKoufens(koufens);
                    }
                }
                @Override
                public void onFailture() {

                }
            },map);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
